# Transparent Corruption: The effect of illicit connections and trusted references 
# on the demand for bureaucratic intermediation

# Authors: Jose Ramon Morales-Arilla & Ana Gabriela Ibarra Luces

# SCRIPT 05 - BALANCE TESTS

# Data --------------------------------------------------------------------

tabla_s_balance <- tabla_survey %>% 
  
  select(Finished, Progress, Q_RelevantIDDuplicateScore ,consent, Y, Sug_Corr, 
         Precio, Exper, Rapidez, GestLink, willing_to_pay, 
         bargain, mig_thought, prev_gest, gest_exp ,gender, 
         age, univ_career, univ_start_yr, current_semest, 
         acad_rank, income_source, m_house_income, survey_chann) %>%
  
  transform(gender = as.character(gender), 
            univ_career = as.character(univ_career), 
            current_semest = as.character(current_semest), 
            acad_rank = as.character(acad_rank), 
            m_house_income = as.character(m_house_income), 
            survey_chann = as.character(survey_chann)) %>% 
  
   # Creating binary variables and categorical variables
   dplyr::mutate(
      
      # Binary of male (1) and female or others (0)
      gender = case_when(gender == 2 ~ 1, 
                         gender == 1 ~ 0,
                         gender == 3 ~ 0, 
                         T ~ as.numeric(gender)),
      
      # Binary of good previous experience with gestores (1) and bad or neutral experience (0)
      gest_exp = case_when(gest_exp == 1 ~ 1,
                           gest_exp == 2 ~ 0,
                           gest_exp ==3 ~ 0, 
                           T ~ as.numeric(gest_exp)),
      
      # Binary of those in early stage of their career (0) and late stage (1) 
      current_semest = case_when(current_semest < 6 ~ 0, 
                                 current_semest > 5 ~ 1, 
                                 T ~ as.numeric(current_semest)),
      
      # Binary of those with high GPA (1) and low GPA (0)
      acad_rank = case_when(acad_rank < 4 ~ 1,
                            acad_rank > 3 ~ 0,
                            T ~ as.numeric(acad_rank)),
      
      # Binary of high monthly household income (1) and low household income (0)
      m_house_income = case_when(m_house_income < 14 ~ 0, 
                                 m_house_income > 13 ~ 1, 
                                 T ~ as.numeric(m_house_income)),
      
      # Categorical variable for faculty
      univ_career = case_when(univ_career == 8 | univ_career == 9 | univ_career == 10 | univ_career == 11 ~ 1, # Engineering 
                              univ_career ==1 | univ_career== 2 | univ_career == 5 | univ_career == 14 | univ_career== 15 ~ 2, # Social science faculty 
                              univ_career == 3 ~ 3, # Communication Management
                              univ_career == 6 | univ_career == 7 | univ_career == 12 | univ_career== 13 ~ 4, # Humanities
                              univ_career == 4 | univ_career == 16 | univ_career == 17 ~ 5, # Law, Architecture, Other
                              T ~ as.numeric(univ_career)),
      
      # Start year of university. 
      univ_start_yr = case_when(univ_start_yr < 2019 ~ 1, # Before 2019
                                univ_start_yr > 2018 ~ 0), # After 2018
      
      ingen = ifelse(univ_career == 1, 1, 0), # Engineering 
      
      faces = ifelse(univ_career == 2, 1, 0), # Social science faculty
      
      comsoc = ifelse(univ_career == 3, 1, 0),  # Communication Management
      
      human = ifelse(univ_career == 4, 1, 0), # Humanities
      
      derech_otr = ifelse(univ_career == 5, 1, 0), # Law, Architecture, Other
    
   # Inattention measure. Inattentive (1), attentive (0) 
   
   # Take-up is true. When price is low, the amount that the participant is 
   # willing to pay should be lower than the low price (120 USD) when bargaining. 
   # When the price is high, the amount that the participant is willing to pay 
   # should be lower than the high price (300 USD) when bargaining. 
   
    inatt = case_when( Y == 1 & Precio == 0 & willing_to_pay < 120 ~ 0, 
                       Y == 1 & Precio == 1 & willing_to_pay < 300 ~ 0, 
                       
                       Y == 1 & Precio == 0 & willing_to_pay > 120 ~ 1, 
                       Y == 1 & Precio == 1 & willing_to_pay > 300 ~ 1,
                       T ~ 0),
   
   # Qualtric's quality measure binary marker
    quality = ifelse(Q_RelevantIDDuplicateScore < 75, 
                     yes = 1, # Good quality 
                     no = ifelse(Q_RelevantIDDuplicateScore > 75, 0, # Bad quality
                            Q_RelevantIDDuplicateScore)))
    


# CORRUPTION REGRESSIONS --------------------------------------------------

# We generate regressions of each covariate as a function of the treatment variable
# of interest
    
# GENDER
    gender_corr_b <- huxreg(feols(gender ~ Sug_Corr, tabla_s_balance),
                            statistics = c("N. obs." = "nobs", 
                                           "R squared" = "r.squared", "F statistic" = "statistic",
                                           "P value" = "p.value"), 
                            stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                            number_format = 2) 
    
# AGE
    age_corr_b <- huxreg(feols(age ~ Sug_Corr, tabla_s_balance),
                         statistics = c("N. obs." = "nobs", 
                                        "R squared" = "r.squared", "F statistic" = "statistic",
                                        "P value" = "p.value"), 
                         stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                         number_format = 2) 
    
# ENGINEERING FACULTY 
    ingen_corr_b <- huxreg(feols(ingen ~ Sug_Corr, tabla_s_balance),
                           statistics = c("N. obs." = "nobs", 
                                          "R squared" = "r.squared", "F statistic" = "statistic",
                                          "P value" = "p.value"), 
                           stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                           number_format = 2) 

# SOCIAL SCIENCES FACULTY 
    faces_corr_b <- huxreg(feols(faces ~ Sug_Corr, tabla_s_balance),
                           statistics = c("N. obs." = "nobs", 
                                          "R squared" = "r.squared", "F statistic" = "statistic",
                                          "P value" = "p.value"), 
                           stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                           number_format = 2) 
    
# COMMUNICATIONS FACULTY 
    comsoc_corr_b <- huxreg(feols(comsoc ~ Sug_Corr, tabla_s_balance),
                            statistics = c("N. obs." = "nobs", 
                                           "R squared" = "r.squared", "F statistic" = "statistic",
                                           "P value" = "p.value"), 
                            stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                            number_format = 2) 
    
# HUMANITIES FACULTY 
    human_corr_b <- huxreg(feols(human ~ Sug_Corr, tabla_s_balance),
                           statistics = c("N. obs." = "nobs", 
                                          "R squared" = "r.squared", "F statistic" = "statistic",
                                          "P value" = "p.value"), 
                           stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                           number_format = 2) 
    
    
# LAW, ARCHITECTURE AND OTHER CAREERS 
    derech_otr_corr_b <- huxreg(feols(derech_otr ~ Sug_Corr, tabla_s_balance),
                                statistics = c("N. obs." = "nobs", 
                                               "R squared" = "r.squared", "F statistic" = "statistic",
                                               "P value" = "p.value"), 
                                stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                                number_format = 2) 
    
# UNIVERSITY START YEAR 
    ustart_corr_b <- huxreg(feols(univ_start_yr ~ Sug_Corr, tabla_s_balance),
                            statistics = c("N. obs." = "nobs", 
                                           "R squared" = "r.squared", "F statistic" = "statistic",
                                           "P value" = "p.value"), 
                            stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                            number_format = 2) 
    
# CURRENT SEMESTER BINARY MARKER
    semest_corr_b <- huxreg(feols(current_semest ~ Sug_Corr, tabla_s_balance),
                            statistics = c("N. obs." = "nobs", 
                                           "R squared" = "r.squared", "F statistic" = "statistic",
                                           "P value" = "p.value"), 
                            stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                            number_format = 2) 
    
# ACADEMIC RANK 
    rank_corr_b <- huxreg(feols(acad_rank ~ Sug_Corr, tabla_s_balance),
                          statistics = c("N. obs." = "nobs", 
                                         "R squared" = "r.squared", "F statistic" = "statistic",
                                         "P value" = "p.value"), 
                                         stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                          number_format = 2)
    
# INCOME SOURCE
    incomes_corr_b <- huxreg(feols(income_source ~ Sug_Corr, tabla_s_balance),
                             statistics = c("N. obs." = "nobs", 
                                            "R squared" = "r.squared", "F statistic" = "statistic",
                                            "P value" = "p.value"), 
                             stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                             number_format = 2)
    
# MONTHLY HOUSEHOLD INCOME
    mincome_corr_b <- huxreg(feols(m_house_income ~ Sug_Corr, tabla_s_balance),
                             statistics = c("N. obs." = "nobs", 
                                            "R squared" = "r.squared", "F statistic" = "statistic",
                                            "P value" = "p.value"), 
                             stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                             number_format = 2)
    
# INATTENTION MEASURE
    inatt_corr_b <- huxreg(feols(inatt ~ Sug_Corr, tabla_s_balance),
                           statistics = c("N. obs." = "nobs", 
                                          "R squared" = "r.squared", "F statistic" = "statistic",
                                          "P value" = "p.value"), 
                           stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                           number_format = 2)

# QUALTRICS' QUALITY MEASURE
    qual_corr_b <- huxreg(feols(quality ~ Sug_Corr, tabla_s_balance),
                          statistics = c("N. obs." = "nobs", 
                                         "R squared" = "r.squared", "F statistic" = "statistic",
                                         "P value" = "p.value"), 
                          stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                          number_format = 2)
    

# PRICE REGRESSIONS -------------------------------------------------------

# We generate regressions of each covariate as a function of the treatment variable
# of interest
    
# GENDER
    gender_prec_b <- huxreg(feols(gender ~ Precio, tabla_s_balance),
                            statistics = c("N. obs." = "nobs", 
                                           "R squared" = "r.squared", "F statistic" = "statistic",
                                           "P value" = "p.value"), 
                            stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                            number_format = 2) 
# AGE 
    age_prec_b <- huxreg(feols(age ~ Precio, tabla_s_balance),
                         statistics = c("N. obs." = "nobs", 
                                        "R squared" = "r.squared", "F statistic" = "statistic",
                                        "P value" = "p.value"), 
                         stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                         number_format = 2) 

# ENGINEERING FACULTY    
    ingen_prec_b <- huxreg(feols(ingen ~ Precio, tabla_s_balance),
                           statistics = c("N. obs." = "nobs", 
                                          "R squared" = "r.squared", "F statistic" = "statistic",
                                          "P value" = "p.value"), 
                           stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                           number_format = 2) 
# SOCIAL SCIENCES FACULTY     
    faces_prec_b <- huxreg(feols(faces ~ Precio, tabla_s_balance),
                           statistics = c("N. obs." = "nobs", 
                                          "R squared" = "r.squared", "F statistic" = "statistic",
                                          "P value" = "p.value"), 
                           stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                           number_format = 2) 
    
# COMMUNICATIONS FACULTY
    comsoc_prec_b <- huxreg(feols(comsoc ~ Precio, tabla_s_balance),
                            statistics = c("N. obs." = "nobs", 
                                           "R squared" = "r.squared", "F statistic" = "statistic",
                                           "P value" = "p.value"), 
                            stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                            number_format = 2) 
    
# HUMANITIES FACULTY 
    human_prec_b <- huxreg(feols(human ~ Precio, tabla_s_balance),
                           statistics = c("N. obs." = "nobs", 
                                          "R squared" = "r.squared", "F statistic" = "statistic",
                                          "P value" = "p.value"), 
                           stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                           number_format = 2) 
    
# LAW, ARCHITECTURE AND OTHER CAREERS
    derech_otr_prec_b <- huxreg(feols(derech_otr ~ Precio, tabla_s_balance),
                                statistics = c("N. obs." = "nobs", 
                                               "R squared" = "r.squared", "F statistic" = "statistic",
                                               "P value" = "p.value"), 
                                stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                                number_format = 2) 
    
# UNIVERSITY START YEAR
    ustart_prec_b <- huxreg(feols(univ_start_yr ~ Precio, tabla_s_balance),
                            statistics = c("N. obs." = "nobs", 
                                           "R squared" = "r.squared", "F statistic" = "statistic",
                                           "P value" = "p.value"), 
                            stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                            number_format = 2) 
    
# CURRENT SEMESTER BINARY MARKER
    semest_prec_b <- huxreg(feols(current_semest ~ Precio, tabla_s_balance),
                            statistics = c("N. obs." = "nobs", 
                                           "R squared" = "r.squared", "F statistic" = "statistic",
                                           "P value" = "p.value"), 
                            stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                            number_format = 2) 
    
# ACADEMIC RANK
    rank_prec_b <- huxreg(feols(acad_rank ~ Precio, tabla_s_balance),
                          statistics = c("N. obs." = "nobs", 
                                         "R squared" = "r.squared", "F statistic" = "statistic",
                                         "P value" = "p.value"), 
                          stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                          number_format = 2)
    
# INCOME SOURCE
    incomes_prec_b <- huxreg(feols(income_source ~ Precio, tabla_s_balance),
                             statistics = c("N. obs." = "nobs", 
                                            "R squared" = "r.squared", "F statistic" = "statistic",
                                            "P value" = "p.value"), 
                             stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                             number_format = 2)
    
# MONTHLY HOUSEHOLD INCOME
    mincome_prec_b <- huxreg(feols(m_house_income ~ Precio, tabla_s_balance),
                             statistics = c("N. obs." = "nobs", 
                                            "R squared" = "r.squared", "F statistic" = "statistic",
                                            "P value" = "p.value"), 
                             stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                             number_format = 2)
    
# INATTENTION MEASURE
    inatt_prec_b <- huxreg(feols(inatt ~ Precio, tabla_s_balance),
                           statistics = c("N. obs." = "nobs", 
                                          "R squared" = "r.squared", "F statistic" = "statistic",
                                          "P value" = "p.value"), 
                           stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                           number_format = 2)
    
# QUALTRICS' QUALITY MEASURE
    qual_prec_b <- huxreg(feols(quality ~ Precio, tabla_s_balance),
                          statistics = c("N. obs." = "nobs", 
                                         "R squared" = "r.squared", "F statistic" = "statistic",
                                         "P value" = "p.value"), 
                          stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                          number_format = 2)
    


# REFERENCE REGRESSIONS ---------------------------------------------------

    
# GENDER
    gender_gestlink_b <- huxreg(feols(gender ~ GestLink, tabla_s_balance),
                                statistics = c("N. obs." = "nobs", 
                                               "R squared" = "r.squared", "F statistic" = "statistic",
                                               "P value" = "p.value"), 
                                stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                                number_format = 2) 
# AGE
    age_gestlink_b <- huxreg(feols(age ~ GestLink, tabla_s_balance),
                             statistics = c("N. obs." = "nobs", 
                                            "R squared" = "r.squared", "F statistic" = "statistic",
                                            "P value" = "p.value"), 
                             stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                             number_format = 2) 
    
# ENGINEERING FACULTY 
    ingen_gestlink_b <- huxreg(feols(ingen ~ GestLink, tabla_s_balance),
                               statistics = c("N. obs." = "nobs", 
                                              "R squared" = "r.squared", "F statistic" = "statistic",
                                              "P value" = "p.value"), 
                               stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                               number_format = 2) 

# SOCIAL SCIENCES FACULTY 
    faces_gestlink_b <- huxreg(feols(faces ~ GestLink, tabla_s_balance),
                               statistics = c("N. obs." = "nobs", 
                                              "R squared" = "r.squared", "F statistic" = "statistic",
                                              "P value" = "p.value"), 
                               stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                               number_format = 2) 
    
# COMMUNICATIONS FACULTY
    comsoc_gestlink_b <- huxreg(feols(comsoc ~ GestLink, tabla_s_balance),
                                statistics = c("N. obs." = "nobs", 
                                               "R squared" = "r.squared", "F statistic" = "statistic",
                                               "P value" = "p.value"), 
                                stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                                number_format = 2) 
    
# HUMANITIES FACULTY 
    human_gestlink_b <- huxreg(feols(human ~ GestLink, tabla_s_balance),
                               statistics = c("N. obs." = "nobs", 
                                              "R squared" = "r.squared", "F statistic" = "statistic",
                                              "P value" = "p.value"), 
                               stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                               number_format = 2) 

# LAW, ARCHITECTURE AND OTHER CAREERS
    derech_otr_gestlink_b <- huxreg(feols(derech_otr ~ GestLink, tabla_s_balance),
                                    statistics = c("N. obs." = "nobs", 
                                                   "R squared" = "r.squared", "F statistic" = "statistic",
                                                   "P value" = "p.value"), 
                                    stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                                    number_format = 2) 
    
# UNIVERSITY START YEAR
    ustart_gestlink_b <- huxreg(feols(univ_start_yr ~ GestLink, tabla_s_balance),
                                statistics = c("N. obs." = "nobs", 
                                               "R squared" = "r.squared", "F statistic" = "statistic",
                                               "P value" = "p.value"), 
                                stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                                number_format = 2) 
    
# CURRENT SEMESTER BINARY MARKER
    semest_gestlink_b <- huxreg(feols(current_semest ~ GestLink, tabla_s_balance),
                                statistics = c("N. obs." = "nobs", 
                                               "R squared" = "r.squared", "F statistic" = "statistic",
                                               "P value" = "p.value"), 
                                stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                                number_format = 2) 
    
# ACADEMIC RANK
    rank_gestlink_b <- huxreg(feols(acad_rank ~ GestLink, tabla_s_balance),
                              statistics = c("N. obs." = "nobs", 
                                             "R squared" = "r.squared", "F statistic" = "statistic",
                                             "P value" = "p.value"), 
                              stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                              number_format = 2)
    
# INCOME SOURCE
    incomes_gestlink_b <- huxreg(feols(income_source ~ GestLink, tabla_s_balance),
                                 statistics = c("N. obs." = "nobs", 
                                                "R squared" = "r.squared", "F statistic" = "statistic",
                                                "P value" = "p.value"), 
                                 stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                                 number_format = 2)
    
# MONTHLY HOUSEHOLD INCOME
    mincome_gestlink_b <- huxreg(feols(m_house_income ~ GestLink, tabla_s_balance),
                                 statistics = c("N. obs." = "nobs", 
                                                "R squared" = "r.squared", "F statistic" = "statistic",
                                                "P value" = "p.value"), 
                                 stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                                 number_format = 2)
    
# INATTENTION MEASURE
    inatt_gestlink_b <- huxreg(feols(inatt ~ GestLink, tabla_s_balance),
                               statistics = c("N. obs." = "nobs", 
                                              "R squared" = "r.squared", "F statistic" = "statistic",
                                              "P value" = "p.value"), 
                               stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                               number_format = 2)
    
# QUALTRICS' QUALITY MEASURE
    qual_gestlink_b <- huxreg(feols(quality ~ GestLink, tabla_s_balance),
                              statistics = c("N. obs." = "nobs", 
                                             "R squared" = "r.squared", "F statistic" = "statistic",
                                             "P value" = "p.value"), 
                              stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                              number_format = 2)
    

# EXPERIENCE REGRESSIONS --------------------------------------------------

# GENDER
    gender_exper_b <- huxreg(feols(gender ~ Exper, tabla_s_balance),
                             statistics = c("N. obs." = "nobs", 
                                            "R squared" = "r.squared", "F statistic" = "statistic",
                                            "P value" = "p.value"), 
                             stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                             number_format = 2) #Significativo
    
# AGE
    age_exper_b <- huxreg(feols(age ~ Exper, tabla_s_balance),
                          statistics = c("N. obs." = "nobs", 
                                         "R squared" = "r.squared", "F statistic" = "statistic",
                                         "P value" = "p.value"), 
                          stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                          number_format = 2) 
    
# ENGINEERING FACULTY 
    ingen_exper_b <- huxreg(feols(ingen ~ Exper, tabla_s_balance),
                            statistics = c("N. obs." = "nobs", 
                                           "R squared" = "r.squared", "F statistic" = "statistic",
                                           "P value" = "p.value"), 
                            stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                            number_format = 2) 
    
# SOCIAL SCIENCES FACULTY 
    faces_exper_b <- huxreg(feols(faces ~ Exper, tabla_s_balance),
                            statistics = c("N. obs." = "nobs", 
                                           "R squared" = "r.squared", "F statistic" = "statistic",
                                           "P value" = "p.value"), 
                            stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                            number_format = 2) 
    
# COMMUNICATIONS FACULTY
    comsoc_exper_b <- huxreg(feols(comsoc ~ Exper, tabla_s_balance),
                             statistics = c("N. obs." = "nobs", 
                                            "R squared" = "r.squared", "F statistic" = "statistic",
                                            "P value" = "p.value"), 
                             stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                             number_format = 2) 
    
# HUMANITIES FACULTY 
    human_exper_b <- huxreg(feols(human ~ Exper, tabla_s_balance),
                            statistics = c("N. obs." = "nobs", 
                                           "R squared" = "r.squared", "F statistic" = "statistic",
                                           "P value" = "p.value"), 
                            stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                            number_format = 2) 
    
# LAW, ARCHITECTURE AND OTHER CAREERS
    derech_otr_exper_b <- huxreg(feols(derech_otr ~ Exper, tabla_s_balance),
                                 statistics = c("N. obs." = "nobs", 
                                                "R squared" = "r.squared", "F statistic" = "statistic",
                                                "P value" = "p.value"), 
                                 stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                                 number_format = 2) 
    
# UNIVERSITY START YEAR
    ustart_exper_b <- huxreg(feols(univ_start_yr ~ Exper, tabla_s_balance),
                             statistics = c("N. obs." = "nobs", 
                                            "R squared" = "r.squared", "F statistic" = "statistic",
                                            "P value" = "p.value"), 
                             stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                             number_format = 2) 
    
# CURRENT SEMESTER BINARY MARKER
    semest_exper_b <- huxreg(feols(current_semest ~ Exper, tabla_s_balance),
                             statistics = c("N. obs." = "nobs", 
                                            "R squared" = "r.squared", "F statistic" = "statistic",
                                            "P value" = "p.value"), 
                             stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                             number_format = 2) 
    
# ACADEMIC RANK
    rank_exper_b <- huxreg(feols(acad_rank ~ Exper, tabla_s_balance),
                           statistics = c("N. obs." = "nobs", 
                                          "R squared" = "r.squared", "F statistic" = "statistic",
                                          "P value" = "p.value"), 
                           stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                           number_format = 2)
    
# INCOME SOURCE
    incomes_exper_b <- huxreg(feols(income_source ~ Exper, tabla_s_balance),
                              statistics = c("N. obs." = "nobs", 
                                             "R squared" = "r.squared", "F statistic" = "statistic",
                                             "P value" = "p.value"), 
                              stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                              number_format = 2) 
    
# MONTHLY HOUSEHOLD INCOME
    mincome_exper_b <- huxreg(feols(m_house_income ~ Exper, tabla_s_balance),
                              statistics = c("N. obs." = "nobs", 
                                             "R squared" = "r.squared", "F statistic" = "statistic",
                                             "P value" = "p.value"), 
                              stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                              number_format = 2)
    
# INATTENTION MEASURE
    inatt_exper_b <- huxreg(feols(inatt ~ Exper, tabla_s_balance),
                            statistics = c("N. obs." = "nobs", 
                                           "R squared" = "r.squared", "F statistic" = "statistic",
                                           "P value" = "p.value"), 
                            stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                            number_format = 2)
    
# QUALTRICS' QUALITY MEASURE
    qual_exper_b <- huxreg(feols(quality ~ Exper, tabla_s_balance),
                           statistics = c("N. obs." = "nobs", 
                                          "R squared" = "r.squared", "F statistic" = "statistic",
                                          "P value" = "p.value"), 
                           stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                           number_format = 2)
    
    

# SPEED REGRESSIONS -------------------------------------------------------

    
# GENDER
    gender_rapid_b <- huxreg(feols(gender ~ Rapidez, tabla_s_balance),
                             statistics = c("N. obs." = "nobs", 
                                            "R squared" = "r.squared", "F statistic" = "statistic",
                                            "P value" = "p.value"), 
                             stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                             number_format = 2) 
# AGE
    age_rapid_b <- huxreg(feols(age ~ Rapidez, tabla_s_balance),
                          statistics = c("N. obs." = "nobs", 
                                         "R squared" = "r.squared", "F statistic" = "statistic",
                                         "P value" = "p.value"), 
                          stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                          number_format = 2) 
    
# ENGINEERING FACULTY 
    ingen_rapid_b <- huxreg(feols(ingen ~ Rapidez, tabla_s_balance),
                            statistics = c("N. obs." = "nobs", 
                                           "R squared" = "r.squared", "F statistic" = "statistic",
                                           "P value" = "p.value"), 
                            stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                            number_format = 2) 

# SOCIAL SCIENCES FACULTY 
    faces_rapid_b <- huxreg(feols(faces ~ Rapidez, tabla_s_balance),
                            statistics = c("N. obs." = "nobs", 
                                           "R squared" = "r.squared", "F statistic" = "statistic",
                                           "P value" = "p.value"), 
                            stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                            number_format = 2) 
    
# COMMUNICATIONS FACULTY
    comsoc_rapid_b <- huxreg(feols(comsoc ~ Rapidez, tabla_s_balance),
                             statistics = c("N. obs." = "nobs", 
                                            "R squared" = "r.squared", "F statistic" = "statistic",
                                            "P value" = "p.value"), 
                             stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                             number_format = 2) 
    
# HUMANITIES FACULTY 
    human_rapid_b <- huxreg(feols(human ~ Rapidez, tabla_s_balance),
                            statistics = c("N. obs." = "nobs", 
                                           "R squared" = "r.squared", "F statistic" = "statistic",
                                           "P value" = "p.value"), 
                            stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                            number_format = 2) 
    
# LAW, ARCHITECTURE AND OTHER CAREERS
    derech_otr_rapid_b <- huxreg(feols(derech_otr ~ Rapidez, tabla_s_balance),
                                 statistics = c("N. obs." = "nobs", 
                                                "R squared" = "r.squared", "F statistic" = "statistic",
                                                "P value" = "p.value"), 
                                 stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                                 number_format = 2) 
    
# UNIVERSITY START YEAR
    ustart_rapid_b <- huxreg(feols(univ_start_yr ~ Rapidez, tabla_s_balance),
                             statistics = c("N. obs." = "nobs", 
                                            "R squared" = "r.squared", "F statistic" = "statistic",
                                            "P value" = "p.value"), 
                             stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                             number_format = 2) 
    
# CURRENT SEMESTER BINARY MARKER
    semest_rapid_b <- huxreg(feols(current_semest ~ Rapidez, tabla_s_balance),
                             statistics = c("N. obs." = "nobs", 
                                            "R squared" = "r.squared", "F statistic" = "statistic",
                                            "P value" = "p.value"), 
                             stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                             number_format = 2) 
    
# ACADEMIC RANK
    rank_rapid_b <- huxreg(feols(acad_rank ~ Rapidez, tabla_s_balance),
                           statistics = c("N. obs." = "nobs", 
                                          "R squared" = "r.squared", "F statistic" = "statistic",
                                          "P value" = "p.value"), 
                           stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                           number_format = 2)
    
# INCOME SOURCE
    incomes_rapid_b <- huxreg(feols(income_source ~ Rapidez, tabla_s_balance),
                              statistics = c("N. obs." = "nobs", 
                                             "R squared" = "r.squared", "F statistic" = "statistic",
                                             "P value" = "p.value"), 
                              stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                              number_format = 2)
    
# MONTHLY HOUSEHOLD INCOME
    mincome_rapid_b <- huxreg(feols(m_house_income ~ Rapidez, tabla_s_balance),
                              statistics = c("N. obs." = "nobs", 
                                             "R squared" = "r.squared", "F statistic" = "statistic",
                                             "P value" = "p.value"), 
                              stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                              number_format = 2)
    
# INATTENTION MEASURE
    inatt_rapid_b <- huxreg(feols(inatt ~ Rapidez, tabla_s_balance),
                            statistics = c("N. obs." = "nobs", 
                                           "R squared" = "r.squared", "F statistic" = "statistic",
                                           "P value" = "p.value"), 
                            stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                            number_format = 2)
    
# QUALTRICS' QUALITY MEASURE
    qual_rapid_b <- huxreg(feols(quality ~ Rapidez, tabla_s_balance),
                           statistics = c("N. obs." = "nobs", 
                                          "R squared" = "r.squared", "F statistic" = "statistic",
                                          "P value" = "p.value"), 
                           stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01),
                            number_format = 2)


# BALANCE TABLE MATRIX ----------------------------------------------------

# Create auxiliary matrix for balance table 
aux <- matrix(nrow = 28, ncol = 5)
colnames(aux) <- c("Corruption", "Price","Reference","Experience","Speed")

rownames(aux) <- c("Gender"," " , "Age", " " , "Ingeniería", " " , "FACES", " " ,
                  "Comunicación Social", " " ,"Humanidades", " " ,"Derecho y otros", " " ,
                  "Start univ.",
                  " " ,"Semester"," " ,"Acad. rank"," " , "Income source",
                  " " ,"Monthly income"," ", "Inattention", " ", "Qualtrics quality", 
                  " ")

# Transform matrix as data.frame
matriz_final <- data.frame(aux) 


## EXTRACT FROM EACH REGRESSION TABLE THE BETA 1 COEFFICIENT AND SE 

# CORRUPTION REGRESSIONS 
mat_bc_corr <- c(
  as.character(gender_corr_b[4,2]),(as.character(gender_corr_b[5,2])),
  
  as.character(age_corr_b[4,2]), (as.character(age_corr_b[5,2])),
  
  as.character(ingen_corr_b[4,2]), (as.character(ingen_corr_b[5,2])),
  
  as.character(faces_corr_b[4,2]), (as.character(faces_corr_b[5,2])),
  
  as.character(comsoc_corr_b[4,2]), (as.character(comsoc_corr_b[5,2])),
  
  as.character(human_corr_b[4,2]), (as.character(human_corr_b[5,2])),
  
  as.character(derech_otr_corr_b[4,2]), (as.character(derech_otr_corr_b[5,2])),
  
  as.character(ustart_corr_b[4,2]), (as.character(ustart_corr_b[5,2])),
  
  as.character(semest_corr_b[4,2]), (as.character(semest_corr_b[5,2])),
  
  as.character(rank_corr_b[4,2]), (as.character(rank_corr_b[5,2])),
  
  as.character(incomes_corr_b[4,2]), (as.character(incomes_corr_b[5,2])),
  
  as.character(mincome_corr_b[4,2]), (as.character(mincome_corr_b[5,2])),
  
  as.character(inatt_corr_b[4,2]), (as.character(inatt_corr_b[5,2])),
  
  as.character(qual_corr_b[4,2]), (as.character(qual_corr_b[5,2])))


# PRICE REGRESSIONS 
mat_bc_prec <- c(
  as.character(gender_prec_b[4,2]),(as.character(gender_prec_b[5,2])),
  
  as.character(age_prec_b[4,2]), (as.character(age_prec_b[5,2])),
  
  as.character(ingen_prec_b[4,2]), (as.character(ingen_prec_b[5,2])),
  
  as.character(faces_prec_b[4,2]), (as.character(faces_prec_b[5,2])),
  
  as.character(comsoc_prec_b[4,2]), (as.character(comsoc_prec_b[5,2])),
  
  as.character(human_prec_b[4,2]), (as.character(human_prec_b[5,2])),
  
  as.character(derech_otr_prec_b[4,2]), (as.character(derech_otr_prec_b[5,2])),
  
  as.character(ustart_prec_b[4,2]), (as.character(ustart_prec_b[5,2])),
  
  as.character(semest_prec_b[4,2]), (as.character(semest_prec_b[5,2])),
  
  as.character(rank_prec_b[4,2]), (as.character(rank_prec_b[5,2])),
  
  as.character(incomes_prec_b[4,2]), (as.character(incomes_prec_b[5,2])),
  
  as.character(mincome_prec_b[4,2]), (as.character(mincome_prec_b[5,2])),
  
  as.character(inatt_prec_b[4,2]), (as.character(inatt_prec_b[5,2])),
  
  as.character(qual_prec_b[4,2]), (as.character(qual_prec_b[5,2])))

# REFERENCE REGRESSIONS 
mat_bc_gestlink <- c(
  as.character(gender_gestlink_b[4,2]),(as.character(gender_gestlink_b[5,2])),
  
  as.character(age_gestlink_b[4,2]), (as.character(age_gestlink_b[5,2])),
  
  as.character(ingen_gestlink_b[4,2]), (as.character(ingen_gestlink_b[5,2])),
  
  as.character(faces_gestlink_b[4,2]), (as.character(faces_gestlink_b[5,2])),
  
  as.character(comsoc_gestlink_b[4,2]), (as.character(comsoc_gestlink_b[5,2])),
  
  as.character(human_gestlink_b[4,2]), (as.character(human_gestlink_b[5,2])),
  
  as.character(derech_otr_gestlink_b[4,2]),(as.character(derech_otr_gestlink_b[5,2])),
  
  as.character(ustart_gestlink_b[4,2]), (as.character(ustart_gestlink_b[5,2])),
  
  as.character(semest_gestlink_b[4,2]), (as.character(semest_gestlink_b[5,2])),
  
  as.character(rank_gestlink_b[4,2]), (as.character(rank_gestlink_b[5,2])),
  
  as.character(incomes_gestlink_b[4,2]), (as.character(incomes_gestlink_b[5,2])),
  
  as.character(mincome_gestlink_b[4,2]), (as.character(mincome_gestlink_b[5,2])),
  
  as.character(inatt_gestlink_b[4,2]), (as.character(inatt_gestlink_b[5,2])),
  
  as.character(qual_gestlink_b[4,2]), (as.character(qual_gestlink_b[5,2])))

# EXPERIENCE REGRESSIONS 
mat_bc_exper <- c(
  as.character(gender_exper_b[4,2]),(as.character(gender_exper_b[5,2])),
  
  as.character(age_exper_b[4,2]), (as.character(age_exper_b[5,2])),
  
  as.character(ingen_exper_b[4,2]), (as.character(ingen_exper_b[5,2])),
  
  as.character(faces_exper_b[4,2]), (as.character(faces_exper_b[5,2])),
  
  as.character(comsoc_exper_b[4,2]), (as.character(comsoc_exper_b[5,2])),
  
  as.character(human_exper_b[4,2]), (as.character(human_exper_b[5,2])),
  
  as.character(derech_otr_exper_b[4,2]), (as.character(derech_otr_exper_b[5,2])),
  
  as.character(ustart_exper_b[4,2]), (as.character(ustart_exper_b[5,2])),
  
  as.character(semest_exper_b[4,2]), (as.character(semest_exper_b[5,2])),
  
  as.character(rank_exper_b[4,2]), (as.character(rank_exper_b[5,2])),
  
  as.character(incomes_exper_b[4,2]), (as.character(incomes_exper_b[5,2])),
  
  as.character(mincome_exper_b[4,2]), (as.character(mincome_exper_b[5,2])),
  
  as.character(inatt_exper_b[4,2]), (as.character(inatt_exper_b[5,2])),
  
  as.character(qual_exper_b[4,2]), (as.character(qual_exper_b[5,2])))


# SPEED REGRESSIONS 
mat_bc_rapid <- c(
  as.character(gender_rapid_b[4,2]),(as.character(gender_rapid_b[5,2])),
  
  as.character(age_rapid_b[4,2]), (as.character(age_rapid_b[5,2])),
  
  as.character(ingen_rapid_b[4,2]), (as.character(ingen_rapid_b[5,2])),
  
  as.character(faces_rapid_b[4,2]), (as.character(faces_rapid_b[5,2])),
  
  as.character(comsoc_rapid_b[4,2]), (as.character(comsoc_rapid_b[5,2])),
  
  as.character(human_rapid_b[4,2]), (as.character(human_rapid_b[5,2])),
  
  as.character(derech_otr_rapid_b[4,2]), (as.character(derech_otr_rapid_b[5,2])),
  
  as.character(ustart_rapid_b[4,2]), (as.character(ustart_rapid_b[5,2])),
  
  as.character(semest_rapid_b[4,2]), (as.character(semest_rapid_b[5,2])),
  
  as.character(rank_rapid_b[4,2]), (as.character(rank_rapid_b[5,2])),
  
  as.character(incomes_rapid_b[4,2]), (as.character(incomes_rapid_b[5,2])),
  
  as.character(mincome_rapid_b[4,2]), (as.character(mincome_rapid_b[5,2])),
  
  as.character(inatt_rapid_b[4,2]), (as.character(inatt_rapid_b[5,2])),
  
  as.character(qual_rapid_b[4,2]), (as.character(qual_rapid_b[5,2])))

                 
# Generate covariates variable and labels
matriz_final$Covariables <- ""  
matriz_final$Covariables <- c("Gender"," " , "Age", " " , "Engeneering", " " , "Social sciences", " " ,
                              "Communications", " " ,"Humanities", " " ,"Law and others", " " ,
                              "Start univ.",
                              " " ,"Semester"," " ,"Acad. rank"," " , "Income source",
                              " " ,"Monthly income"," ", "Inattention", " ", "Qualtrics quality", 
                              " ")

# Assign each coefficient and SE vector to the corresponding treatment variable
matriz_final$Corruption <- mat_bc_corr
matriz_final$Price <- mat_bc_prec
matriz_final$Reference <- mat_bc_gestlink
matriz_final$Experience <- mat_bc_exper
matriz_final$Speed <- mat_bc_rapid

# Reorganize matrix variables
matriz_final <- matriz_final[, c(6,1:5)]

# Transform to huxtable 
Matriz_Final <- huxtable(matriz_final)


# BALANCE TABLE TRANSFORMATION  -------------------------------------------

# The following loop rounds each value to 4 decimal numbers, maintaining 
# significance stars and SE parentheses

for (i in 2:nrow(Matriz_Final)){
   for(j in 2:length(Matriz_Final)){
      
      if(i %% 2 == 0){
      if(grepl("\\*\\*\\*", Matriz_Final[i, j])){
         
         Matriz_Final[i, j] <- paste0(round(parse_number(as.character(Matriz_Final[i, j])), digits = 4), '***')
         
      }else{if(grepl("\\*\\*", Matriz_Final[i, j])){
            
         Matriz_Final[i, j] <- paste0(round(parse_number(as.character(Matriz_Final[i, j])), digits = 4), '**')
            
         }else{if(grepl("\\*", Matriz_Final[i, j])){
            
         
         Matriz_Final[i, j] <- paste0(round(parse_number(as.character(Matriz_Final[i, j])), digits = 4), '*')
               
               
         }else{
            
            Matriz_Final[i, j] <- as.character(round(parse_number(as.character(Matriz_Final[i, j])), digits = 4))
               
            }
         }
      }}else{
         
         Matriz_Final[i, j] <- paste0('(',round(parse_number(as.character(Matriz_Final[i, j])), digits = 4), ')')
      }
      
   }
   }


Matriz_Final <- Matriz_Final[1:25,]

# EXPORT ------------------------------------------------------------------

write_xlsx(Matriz_Final, 'Data/Output/Tables/05_balance_tests_table.xlsx')

print(xtable(Matriz_Final, type = "latex"), include.colnames = F, include.rownames = F, file = "Data/Output/Tables/05_balance_tests_table.tex")

# ## ----------------------------------------------------------------------

rm(list = setdiff(ls(), c('tabla_survey', 'tab_1', 'tab_2', 'tab_3', 'tab_4', 
                          'tab_1_q', 'tab_2_q', 'tab_3_q', 'tab_h', 'tab_sq', 
                          'reg_1_c', 'reg_5_c', 'reg_9_c', 'reg_1_s', 'reg_1_h', 
                          'Tabla_Att', 'Matriz_Final')))
